#include "StdAfx.h"
#include "ResManager.h"
#include "Elements.h"
#include "Game.h"
#include "DrawWindow.h"


void AnimaObj::Draw( ){
	if(!hBm) return;
	SelectObject(W->EmptyDC, hBm); 
	TransparentBlt(W->BackBufferDC, X, Y, Width, Height, W->EmptyDC, 
				XSrc, YSrc, WidthSrc, HeightSrc, 0x00FF00FF);
}

void AnimaObj::Load(int id, int nFrame_i){
	SIZE size;
	_ASSERT(id > 0x10100);

	nFrame = nFrame_i;
	if(!nFrame) return;

	hBm = G.RM->LoadBmp(id);
	if(!hBm) return;

	GetBitmapSize(hBm, size);
	FullWidth = size.cx;
	if(!FullWidth) {
		G.RM->UnloadBmp(hBm);
	}
	Height = HeightSrc = size.cy;
	Width = WidthSrc = FullWidth / nFrame;
}

void AnimaObj::Unload(){
	G.RM->UnloadBmp(hBm);
	G.RM->UnloadBmp(hBmFlip);
}

extern char str[100];
bool AnimaObj::IncreaseFrameByTime(double Time, bool Loop_i){
	if(!hBm) return false;
	double Elipsed =Time - TimeLastFrame;
	if(Elipsed < 0){Elipsed = 0; TimeLastFrame = Time;}

	if(Elipsed >= Speed){
		if(IncreaseFrame(Loop_i)){
			TimeLastFrame = Time;
			return true;
		}
	}
	return false;
}

bool AnimaObj::IncreaseFrame(bool Loop_i){
	if(!hBm) return false;
	if(NowFrame< nFrame){
		NowFrame++;
		SetCurrentFrame(NowFrame); 
		return true;
	}else{
		if(Loop_i){
			SetCurrentFrame(1);
			return true;
		}
	}
	return false;
}

void AnimaObj::FacingLeft(){
	if(!hBm) return;
	if(!hBmFlip){
		hBmFlip = G.RM->CreateBufferBmp(FullWidth, HeightSrc);
		HDC tDC = W->CreateEmptyDC();
		SelectObject(tDC, hBmFlip);
		W->Put2EmptyDC(hBm);

		int Frame = NowFrame;
			SetCurrentFrame(1);
		//Flip every frame... 
		for(int i=0; i< nFrame; i++){
			StretchBlt(tDC, ((i + 1) * WidthSrc) - 1, 0, -WidthSrc, HeightSrc,
				W->EmptyDC, XSrc, YSrc, WidthSrc, HeightSrc, SRCCOPY);
			IncreaseFrame(false);
		}
		W->DeleteEmptyDC(tDC);
		NowFrame = Frame;
	}//if(!hBmFlip)
	
	//exchange :
	HBITMAP t = hBm;
	hBm = hBmFlip;
	hBmFlip = t;
	ToLeft = !ToLeft;
}


AnimaObj::AnimaObj(){
	nFrame = 1;
	Loop = true;
	ToLeft = false;
	Speed = 1.0/16.0;
	TimeLastFrame = 0;
	hBm = 0; 
	hBmFlip = 0; 
	NowFrame = 1; 
	FullWidth = 0;
	XSrc = YSrc = WidthSrc = HeightSrc = 0;
	Width = Height = 0;
	X = Y = 0;
}



////////////////////////////////////////////////////////////////
ObjectInfo::ObjectInfo(ResourceManager* RM){
	_ASSERT(RM);

	nFighter = 0;
	nBkgnd = 0;
	memset(FName, 0, nFtrObj * (MaxFNameLen+1) * sizeof(WCHAR));
	memset(BName, 0, nFtrObj * (MaxBNameLen+1) * sizeof(WCHAR));

	//make up the info 
	WCHAR* p = RM->LoadObjStr(0xFFF);

	//load Fighter Infomation
	int FighterID = _wtoi(p);
	if(!FighterID) return; //if == 0 (not exist)

	int Fcnt = 0;//fighter count

	while(FighterID < 201 && FighterID > 100){//legal Fighter ID
		if(Fcnt >= MaxObj_Idx ){
			SO::Move2NextLine(p);
			FighterID = _wtoi(p);
			continue;
		}

		Fcnt ++;
		SO::Move2NextWord(p);
		int Ncnt = 0;
		while(*p!= 0x0A && Ncnt < MaxFNameLen){
			FName[Fcnt][Ncnt] = *p;
			Ncnt++;
			p++;
		}
		SO::Skip1(p);
		FighterID = _wtoi(p);
	}
	nFighter = Fcnt;

	//Load Background infomation
	int BkgndID = FighterID;
	if(!FighterID) return; //if == 0 (not exist)
	int Bcnt = 0;
	while(BkgndID < 301 && BkgndID > 200){//legal Fighter ID
		if(Bcnt >= MaxObj_Idx ){
			SO::Move2NextLine(p);
			BkgndID = _wtoi(p);
			continue;
		}

		Bcnt ++;
		SO::Move2NextWord(p);
		int Ncnt = 0;
		while(*p!= 0x0A && Ncnt < MaxBNameLen ){
			BName[Bcnt][Ncnt] = *p;
			Ncnt++;
			p++;
		}
		SO::Skip1(p);
		BkgndID = _wtoi(p);
	}
	nBkgnd = Bcnt;

	RM->UnloadObjStr(0xFFF);

	//overloading happens
	_ASSERT(*FName[ObjectArrayLast] == 0);
	_ASSERT(*BName[ObjectArrayLast] == 0);
}

////////////////////////////////////////////////////////////////
void GetBitmapSize(HBITMAP hbmp, SIZE & size){
	BITMAP	bm;
	int a = GetObjectW(hbmp, sizeof(BITMAP), &bm);
	size.cx = bm.bmWidth;
	size.cy = bm.bmHeight;
}


void GetBitmapSize(HBITMAP hbmp, int& W_io, int& H_io){
	BITMAP	bm;
	int bytes = GetObjectW(hbmp, sizeof(BITMAP), &bm);
	if(!bytes){
		W_io = H_io = 0;
	}else{
		W_io = bm.bmWidth;
		H_io = bm.bmHeight;
	}
}